package com.czk.tx.service;

import com.czk.jdbctemplate.entity.Count;
import com.czk.tx.dao.RepeableDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 可重复读下幻读的演示
 * <p>
 * 事务1	                            事务2
 * T1	select * from test;
 * T2	 	                             update test set age=12 where id=1;
 * T3	update test set age=age+1 where id=1;
 * T4	select * from test;
 *
 * @Author:ChenZhangKun
 * @Date: 2022/1/6 16:49
 */
@Service
public class RepeatAbleService {
    @Autowired
    private RepeableDao repeableDao;

    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public void select() {
        List<Count> select = repeableDao.select();
        System.out.println(select);
        // 调用事务二去修改数据

        // 事务一再修改一次
        repeableDao.update(1);
        // 再次查询，则修改了两次
        List<Count> select1 = repeableDao.select();
        System.out.println(select1);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public void update() {
        repeableDao.update(1);
    }
}
